package com.coding.http;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.coding.config.AppConfig;
import com.coding.model.ExcelModel;
import com.coding.model.QueryModel;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class ApiService {

    private static final String HOST = AppConfig.SERVICE_URL;

    public static String arrearsList(QueryModel queryModel) {
        return getResult("/arrearsList", queryModel);
    }

    public static String payRecord(QueryModel queryModel) {
        return getResult("/payRecord", queryModel);
    }

    public static String regretDel(QueryModel queryModel) {
        return getResult("/regretDel", queryModel);
    }

    public static String regretList(QueryModel queryModel) {
        return getResult("/regretList", queryModel);
    }

    private static String getResult(String url, QueryModel queryModel) {
        Map<String, Object> body = new HashMap<>();
        body = JSON.parseObject(JSON.toJSONString(queryModel), Map.class);
        String result = HttpUtil.post(HOST + url, body);
        JSONObject jsonObject = JSON.parseObject(result);

        System.out.println("result = " + result);
        if ("0".equals(jsonObject.getString("code"))) {
            result = jsonObject.getString("data");
        }
        if ("500".equals(jsonObject.getString("code"))) {
            result = jsonObject.getString("message");
        }
        return result;
    }

    public static File excelResult(ExcelModel excelModel) {
        Map<String, Object> body = new HashMap<>();
        body = JSON.parseObject(JSON.toJSONString(excelModel), Map.class);
//        File file = new File("d://");
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File file = fsv.getHomeDirectory();
        File result = HttpRequest.get(HOST + "/downExcel").form(body).execute().writeBodyForFile(file, null);
        return result;
    }

    public static File downExcelRecord(ExcelModel excelModel) {
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File file = fsv.getHomeDirectory();
        Map<String, Object> body = new HashMap<>();
        body = JSON.parseObject(JSON.toJSONString(excelModel), Map.class);
        //File file = new File("d://");
        File result = HttpRequest.get(HOST + "/downExcelRecord").form(body).execute().writeBodyForFile(file, null);
        return result;
    }

    public static JSONObject getUser(String userName, String password) {
        JSONObject obj = new JSONObject();
        try {
            Map<String, Object> body = new HashMap<>();
            body.put("account", userName);
            body.put("password", password);
            String address = HOST + "/getUser";
            String result = HttpUtil.post(address, body);
            JSONObject jsonObject = JSON.parseObject(result);

            if ("0".equals(jsonObject.getString("code"))) {
                obj.put("account", jsonObject.getJSONObject("data").getString("name"));
                obj.put("userId", jsonObject.getJSONObject("data").getString("userId"));
            }
            if ("500".equals(jsonObject.getString("code"))) {
                obj.put("account", jsonObject.getString("message"));
            }
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
            obj.put("account", "请求异常,联系管理员");
            return obj;
        }
    }


    /**
     * 修改密码
     *
     * @param userName
     * @param oldPassword
     * @param newPassword
     * @return
     */
    public static String updPassWord(String userName,
                                     String oldPassword,
                                     String newPassword) {
        try {
            Map<String, Object> body = new HashMap<>();
            body.put("account", userName);
            body.put("oldPassword", oldPassword);
            body.put("newPassword", newPassword);

            String address = HOST + "/updPassWord";
            String result = HttpUtil.post(address, body);
            JSONObject jsonObject = JSON.parseObject(result);

            if ("0".equals(jsonObject.getString("code"))) {
                return jsonObject.getString("data");
            }

            if ("500".equals(jsonObject.getString("code"))) {
                return jsonObject.getString("message");
            }

            return "请求异常,联系管理员";
        } catch (Exception e) {
            e.printStackTrace();
            return "请求异常,联系管理员";
        }
    }
}
